package com.joesem.sale.client.logic.event

import android.os.Bundle
import android.view.LayoutInflater
import com.bumptech.glide.Glide
import com.jiaxing.sale.client.databinding.FragmentEventInfoBinding
import com.jiaxing.sale.client.databinding.ItemImgBinding
import com.joesem.sale.client.base.BaseFragment
import com.joesem.sale.client.base.gone
import com.joesem.sale.client.base.visible
import com.joesem.sale.client.network.data.EventDetailsMode
import com.joesem.sale.client.utils.click
import com.joesem.sale.client.widget.dialog.imagePreview

class EventInfoFragment : BaseFragment<FragmentEventInfoBinding>() {
    override fun FragmentEventInfoBinding.initView(savedInstanceState: Bundle?) {

    }

    fun refresh(data: EventDetailsMode) {
        bind.apply {
            tvEventNo.text = data.event_number.orEmpty()
            tvEventType.text = data.type.orEmpty()
            tvProduct.text =
                data.products?.joinToString { it.product?.name.orEmpty() + if (it.product?.model.isNullOrEmpty()) "" else "-${it.product?.model}" }
            tvMsg.text = data.description.orEmpty()
            if (data.images.isNullOrEmpty()) {
                llImgTitle.gone()
                llImage.gone()
            } else {
                llImage.visible()
                llImgTitle.visible()
                llImage.removeAllViews()
                data.images.forEachIndexed { index, image ->
                    val bindItem = ItemImgBinding.inflate(LayoutInflater.from(requireContext()))
                    bindItem.btnImgDelete.gone()
                    bindItem.root.click { v ->
                        imagePreview(
                            requireContext(),
                            childFragmentManager,
                            data.images.map { it.img_url.orEmpty() },
                            index
                        )
                    }
                    Glide.with(requireContext())
                        .load(image.img_url)
                        .into(bindItem.btnImg)
                    llImage.addView(bindItem.root)
                }
            }
            tvChnnal.text = data.created_type.orEmpty()
            tvCreateUser.text = data.creator?.name.orEmpty()
            tvCreateDate.text = data.create_time_format.orEmpty()
            tvCompleteDate.text = data.update_time_format.orEmpty()
        }
    }
}